/*****************************************************************************
 * hal_opl1x_flash.c
 *
 * Copyright (C) 2019 Jeasonvor <1101627719@qq.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/

#include "plum_api.h"
#include "hal/source/hal_opl1x/hal_opl1x.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "hal.flash"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define HAL_EXFLASH_DEFAULT                                            \
    {                                                                  \
        .pin =                                                         \
            {                                                          \
                .mosi_pin = PLUM_HAL_GPIO_ID(A, 1),                    \
                .miso_pin = PLUM_HAL_GPIO_ID(A, 2),                    \
                .clk_pin  = PLUM_HAL_GPIO_ID(A, 3),                    \
            },                                                         \
        .mode = PLUM_HAL_SPI_MODE_0, .freq = 1000000, .word_width = 8, \
    }

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/**
 * 外部spi flash
 **/
typedef struct {
    plum_u32         spi_idx;
    struct list_head list;
} hal_exflash_t;

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
PLUM_PRIVATE hal_exflash_t exflash_head;

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_s32 hal_inter_flash_read(plum_u32 id, plum_u32 offset, plum_u8 *rData,
                              plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    PLUM_UNUSED(id);
    PLUM_UNUSED(offset);
    PLUM_UNUSED(rData);
    PLUM_UNUSED(size);

    return (rc);
}

PLUM_PRIVATE
plum_s32 hal_inter_flash_write(plum_u32 id, plum_u32 offset, plum_u8 *wData,
                               plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    PLUM_UNUSED(id);
    PLUM_UNUSED(offset);
    PLUM_UNUSED(wData);
    PLUM_UNUSED(size);

    return (rc);
}

PLUM_PUBLIC
plum_s32 hal_inter_flash_erase(plum_u32 id, plum_u32 offset, plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    PLUM_UNUSED(id);
    PLUM_UNUSED(offset);
    PLUM_UNUSED(size);

    return (rc);
}

PLUM_PRIVATE
hal_exflash_t *hal_exflash_register(plum_u32 id)
{
    hal_exflash_t *node = plum_null;

    do {
        if (!exflash_head.list.next) {
            INIT_LIST_HEAD(&exflash_head.list);
        }

        node = plum_hal_malloc(sizeof(hal_exflash_t));
        if (!node) {
            LOG_E("plum_hal_malloc err,rc: %d", PLUM_ECODE_EMEM);
            break;
        }

        memset(node, 0, sizeof(hal_exflash_t));

        node->spi_idx = PLUM_HAL_FLASH_IDX(id) - 100;

        list_add(&node->list, &exflash_head.list);
    } while (0);

    return (node);
}

PLUM_PRIVATE
hal_exflash_t *hal_exflash_find(plum_u32 id)
{
    hal_exflash_t *node = plum_null;

    struct list_head *pos;
    list_for_each(pos, &exflash_head.list)
    {
        node = list_entry(pos, hal_exflash_t, list);
        if (node->spi_idx == (PLUM_HAL_FLASH_IDX(id) - 100)) {
            break;
        }
    }

    return (node);
}

PLUM_PRIVATE
plum_void hal_exflash_unregister(hal_exflash_t *node)
{

    do {
        if (!node) {
            break;
        }

        list_del(&node->list);

        plum_hal_free(node);
    } while (0);
}

PLUM_PRIVATE
plum_s32 hal_exter_flash_read(plum_u32 id, plum_u32 offset, plum_u8 *rData,
                              plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        hal_exflash_t *node = hal_exflash_find(id);
        if (!node) {
            plum_u32           spi_id = PLUM_HAL_SPI_ID(PLUM_HAL_FLASH_IDX(id));
            plum_hal_spi_cof_t cof    = HAL_EXFLASH_DEFAULT;
            rc                        = plum_hal_spi_init(spi_id, &cof);
            if (rc) {
                LOG_E("plum_hal_spi_init err,rc: %d", rc);
                rc = PLUM_ECODE_EIO;
                break;
            }

            rc = Hal_Flash_Init(node->spi_idx);
            if (rc) {
                LOG_E("spi flash init err,rc: %d", rc);
                rc = PLUM_ECODE_UNK;
                break;
            }
        }

        rc = Hal_Flash_AddrRead(node->spi_idx, offset, QMODE_ENABLE, size,
                                rData);
        if (rc > 0) {
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PRIVATE
plum_s32 hal_exter_flash_write(plum_u32 id, plum_u32 offset, plum_u8 *wData,
                               plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        hal_exflash_t *node = hal_exflash_find(id);
        if (!node) {
            plum_u32           spi_id = PLUM_HAL_SPI_ID(PLUM_HAL_FLASH_IDX(id));
            plum_hal_spi_cof_t cof    = HAL_EXFLASH_DEFAULT;
            rc                        = plum_hal_spi_init(spi_id, &cof);
            if (rc) {
                LOG_E("plum_hal_spi_init err,rc: %d", rc);
                rc = PLUM_ECODE_EIO;
                break;
            }

            rc = Hal_Flash_Init(node->spi_idx);
            if (rc) {
                LOG_E("spi flash init err,rc: %d", rc);
                rc = PLUM_ECODE_UNK;
                break;
            }
        }

        rc = Hal_Flash_AddrProgram(node->spi_idx, offset, QMODE_ENABLE, size,
                                   wData);
        if (rc > 0) {
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 hal_exter_flash_erase(plum_u32 id, plum_u32 offset, plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        hal_exflash_t *node = hal_exflash_find(id);
        if (!node) {
            plum_u32           spi_id = PLUM_HAL_SPI_ID(PLUM_HAL_FLASH_IDX(id));
            plum_hal_spi_cof_t cof    = HAL_EXFLASH_DEFAULT;
            rc                        = plum_hal_spi_init(spi_id, &cof);
            if (rc) {
                LOG_E("plum_hal_spi_init err,rc: %d", rc);
                rc = PLUM_ECODE_EIO;
                break;
            }

            rc = Hal_Flash_Init(node->spi_idx);
            if (rc) {
                LOG_E("spi flash init err,rc: %d", rc);
                rc = PLUM_ECODE_UNK;
                break;
            }
        }

        rc = Hal_Flash_4KSectorAddrErase(node->spi_idx, offset);
        if (rc > 0) {
            break;
        }

    } while (0);

    return (rc);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_hal_flash_read(plum_u32 id, plum_u32 offset, plum_u8 *rData,
                             plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_FLASH(id)) {
            LOG_E("flash id is invalid:0x%08X", id);
            rc = PLUM_ECODE_INVALID;
            break;
        }

        if (!rData) {
            rc = PLUM_ECODE_EPARA;
            LOG_E("read pointer is null,rc: %d", rc);
            break;
        }

        if (PLUM_HAL_FLASH_IDX(id) < 100) {
            rc = hal_inter_flash_read(id, offset, rData, size);
        }
        else {
            rc = hal_exter_flash_read(id, offset, rData, size);
        }
        if (rc <= 0) {
            LOG_E("hal flash read err,rc: %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_flash_write(plum_u32 id, plum_u32 offset, plum_u8 *wData,
                              plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_FLASH(id)) {
            LOG_E("flash id is invalid:0x%08X", id);
            rc = PLUM_ECODE_INVALID;
            break;
        }

        if (!wData) {
            rc = PLUM_ECODE_EPARA;
            LOG_E("read pointer is null,rc: %d", rc);
            break;
        }

        if (PLUM_HAL_FLASH_IDX(id) < 100) {
            rc = hal_inter_flash_write(id, offset, wData, size);
        }
        else {
            rc = hal_exter_flash_write(id, offset, wData, size);
        }
        if (rc <= 0) {
            LOG_E("hal flash write err,rc: %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_flash_erase(plum_u32 id, plum_u32 offset, plum_u32 size)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_FLASH(id)) {
            LOG_E("flash id is invalid:0x%08X", id);
            rc = PLUM_ECODE_INVALID;
            break;
        }

        if (PLUM_HAL_FLASH_IDX(id) < 100) {
            rc = hal_inter_flash_erase(id, offset, size);
        }
        else {
            rc = hal_exter_flash_erase(id, offset, size);
        }
        if (rc <= 0) {
            LOG_E("hal flash erase err,rc: %d", rc);
            break;
        }
    } while (0);

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
